package com.sandking.db.jedis;

import com.sandking.tools.SK_Plus;
import com.sandking.tools.SK_Jedis;
import java.util.List;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Jedis;
import com.sandking.db.dao.ConfigDao;
import com.sandking.db.bean.Config;

public class ConfigJedis{
	/**
	 * 根据( id ) 查询
	 */
	public static Config getById(int id){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		Config config = null;
		try{
			String key = "Config_Object";
			String field = SK_Plus.b("id:",id).e();
			config = Config.createForJson(jedis.hget(key,field));
			if(config == null){
				config = ConfigDao.getById(id);
				if(config!=null){
					loadCache(config);
				}
			}
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
		
		return config;
	}
	
	/**
	 * 根据( k ) 查询
	 */
	public static Config getByK(String k){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		Config config = null;
		try{
			String key = "Config_Index";
			String field = SK_Plus.b("k:",k).e();
			String primaryKey = jedis.hget(key,field);
			if(primaryKey!=null){
				config = getById(Integer.valueOf(primaryKey));
			}
			if(config == null){
				config = ConfigDao.getByK(k);
				if(config!=null){
					loadCache(config);
				}
			}
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
		
		return config;
	}
	

	public static void loadCache(Config config){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			loadCache(config,p);
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	/**
	 * 加载一个缓存
	 */
	public static void loadCache(Config config,Pipeline p){
		// ---------------------- 主键索引 ----------------------
		String key = "Config_Object";
		String field = SK_Plus.b("id:",config.getId()).e();
		String data = config.toJson();
		p.hset(key,field,data);
		
		// ---------------------- 唯一索引 ----------------------
		String primaryKey = String.valueOf(config.getId());
		key = "Config_Index";
		
		field = SK_Plus.b("k:",config.getK()).e();
		p.hset(key,field,primaryKey);
		
	}
	
	/**
	 * 加载一组缓存
	 */
	public static void loadCaches(List<Config> configs){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			for(Config config : configs){
				loadCache(config,p);
			}
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	public static void clearCache(Config config){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			clearCache(config,p);
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	/**
	 * 清空一个缓存
	 */
	public static void clearCache(Config config,Pipeline p){
		// ---------------------- 主键索引 ----------------------
		String key = "Config_Object";
		String field = SK_Plus.b("id:",config.getId()).e();
		p.hdel(key,field);
		
		// ---------------------- 唯一索引 ----------------------
		key = "Config_Index";
		
		field = SK_Plus.b("k:",config.getK()).e();
		p.hdel(key,field);
		
	}
	/**
	 * 清空一组缓存
	 */
	public static void clearCaches(List<Config> configs){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			for(Config config : configs){
				clearCache(config,p);
			}
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	public static Config insert(Config config){
		config = ConfigDao.insert(config);
		if(config!=null){
			loadCache(config);
		}
    	return config;
    }
    
    public static Config update(Config config){
    	config = ConfigDao.update(config);
    	if(config!=null){
			loadCache(config);
		}
    	return config;
    }
    
    public static boolean delete(Config config){
    	boolean bool = ConfigDao.delete(config);
    	if(bool){
    		clearCache(config);
    	}
    	return bool;
    }
}